<?php
/**
 * Pert Library
 * Link Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert;

class Link
{
	private $name;

	private $target;

	public function __Construct($name, $target = NULL)
	{
		$this->name = (string) $name;

		$this->target = $target;
	}

	public function __ToString()
	{
		return $this->name;
	}

	private $parameterArray = array();

	public function __Get($parameter)
	{
		if(isset($this->parameterArray[$parameter]))
			return $this->parameterArray[$parameter];
	}

	public static function ParameterControl($value)
	{
		if(is_numeric($value))
			return TRUE;

		$valueArray = explode('|', $value);

		foreach($valueArray AS $value)
			if(!is_numeric($value))
				return FALSE;

		return TRUE;
	}

	public function __Set($parameter, $value)
	{
		if(self::ParameterControl($value))
			$this->parameterArray[$parameter] = $value;
	}

	public function __Unset($parameter)
	{
		if(isset($this->parameterArray[$parameter]))
			unset($this->parameterArray[$parameter]);
	}

	private function parametersToHref()
	{
		$count = 0;

		$href = '';

		foreach($this->parameterArray AS $parameter => $value)
		{
			if($count++)
				$href .= '&';
			else
				$href = '?';

			$href .= $parameter . '=' . $value;
		}

		return $href;
	}

	public function Href()
	{
		return $this->name . $this->parametersToHref();
	}

	public function Html($html)
	{
		return '
<a href="' . $this->Href() . '" ' . ($this->target ? 'target="' . $this->target . '" ' : '') . '>
	' . $html . '
</a>';
	}
}
